package no.uib.fragmentation_analyzer.gui;

import javax.swing.JOptionPane;

/**
 * A simple dialog that lets the user provide the minimum number of sequence pairs.
 *
 * @author Harald Barsnes
 */
public class MinimumSequencePairs extends javax.swing.JDialog {

    private FragmentationAnalyzer fragmentationAnalyzer;

    /**
     * Creates a new MinimumSequencePairs dialog.
     *
     * @param fragmentationAnalyzer
     * @param modal
     */
    public MinimumSequencePairs(FragmentationAnalyzer fragmentationAnalyzer, boolean modal) {
        super(fragmentationAnalyzer, modal);
        initComponents();

        this.fragmentationAnalyzer = fragmentationAnalyzer;

        minimumSequencePairsJSpinner.setValue(
                new Integer(fragmentationAnalyzer.getUserProperties().getMinimumIdentificationPairCounter()));

        setLocationRelativeTo(fragmentationAnalyzer);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();
        minimumSequencePairsJSpinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Modification Search");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        okJButton.setText("OK");
        okJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        minimumSequencePairsJSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(30), Integer.valueOf(1), null, Integer.valueOf(1)));

        jLabel2.setText("Provide the minimum number of sequence pairs required:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(okJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelJButton))
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .add(18, 18, 18)
                        .add(minimumSequencePairsJSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(new java.awt.Component[] {cancelJButton, okJButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(minimumSequencePairsJSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelJButton)
                    .add(okJButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Verifies the selected input and then closes the dialog.
     *
     * @param evt
     */
    private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okJButtonActionPerformed
        
        try{
            fragmentationAnalyzer.getUserProperties().setMinimumSequencePairCounter(
                    ((Integer) minimumSequencePairsJSpinner.getValue()).intValue());
            setVisible(true);
            dispose();
        } catch(NumberFormatException e){
            JOptionPane.showMessageDialog(this,
                    "Minimum sequence pairs has to be an integer.",
                    "Minimum Sequence Pairs", JOptionPane.INFORMATION_MESSAGE);
            minimumSequencePairsJSpinner.requestFocus();
        }

}//GEN-LAST:event_okJButtonActionPerformed

    /**
     * Closes the dialog and terminates the load data set process.
     *
     * @param evt
     */
    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        fragmentationAnalyzer.cancelProgress();
        setVisible(true);
        dispose();
}//GEN-LAST:event_cancelJButtonActionPerformed

    /**
     * @see #cancelJButtonActionPerformed(java.awt.event.ActionEvent) 
     */
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        cancelJButtonActionPerformed(null);
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelJButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSpinner minimumSequencePairsJSpinner;
    private javax.swing.JButton okJButton;
    // End of variables declaration//GEN-END:variables
}
